NOTES & EXPLORATIONS
3.聚类模块
1. Kmean
from sklearn.cluster import KMeans
KMeans(n_clusters=8, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=None, algorithm='auto')
参数说明
n_clusters : int
- 选取的n个簇
init : {'k-means++', 'random' or an ndarray}
- 簇位置的初始化方法
- 如果是np.array ,shape= (n_clusters, n_features)
n_init : int, default: 10
- 用不同的聚类中心初始化值运行算法的次数,最终解是在inertia(每个簇内到其质心的距离相加) 意义下选出的最优结果
max_iter : int, default: 300
- 最大迭代次数
tol : float, default: 1e-4
Relative tolerance with regards to inertia to declare convergence
precompute_distances : {'auto', True, False}
- 预计算距离,计算速度更快但占用更多内存。
1. auto:如果 样本数乘以聚类数(n_samples*n_clusters)大于 12 million 的话则不预计算距离。
2. True:总是预先计算距离。
3. False:永远不预先计算距离。
verbose : int, default 0
Verbosity mode.是否输出详细信息,0 不输出
random_state : int, RandomState instance or None (default)
Determines random number generation for centroid initialization. Use
an int to make the randomness deterministic.
See :term:Glossary <random_state>.
copy_x : boolean, optional
When pre-computing distances it is more numerically accurate to center
the data first. If copy_x is True (default), then the original data is
not modified, ensuring X is C-contiguous. If False, the original data
is modified, and put back before the function returns, but small
numerical differences may be introduced by subtracting and then adding
the data mean, in this case it will also not ensure that data is
C-contiguous which may cause a significant slowdown.
n_jobs : int or None, optional (default=None)
- 计算使用的cpu数量
- 默认None=1,-1 表示最大
algorithm : "auto", "full" or "elkan", default="auto"
- 使用的算法
- full 是经典的EM 算法
- elkan elkan 算法利用了两边之和大于等于第三边,以及两边之差小于第三边的三角形性质,来减少距离的计算。
- "auto" 对稀疏矩阵使用elkan 算法,对稠密矩阵使用EM 算法
一个问题,通向下一个问题。
继续探索知识库 →